<?php
include  'loginCheck.php';
include  'Util.php';
include_once 'generateRSS.php';


$link = Util::getLink();

if ($_POST['delete']) {
    $link = Util::getLink();
    for ($i = 1; $i < $_POST['count']+1; $i++) {
        if (isset($_POST['element_1_'.$i])) {
            $query = sprintf(Util::$delete_jornakDestination,
                mysql_real_escape_string($_POST['element_1_'.$i])
            );
            $result = mysql_query($query);

            // Check result
            if (!$result) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $query;
                die($message);
            }
        //echo ("Query succesful. <br>");
        }
    }
    Util::closeLink($link);

    header("location: ".Constants::$link_done_fromResolve);
    exit();
}

if (isset ($_POST['choseSection'])) {

    Util::closeLink($link);
    header("location: ".Constants::$link_editProjects.$_POST['element_1']);
    exit();
}

if (isset ($_POST['newSection'])) {
// zapis textu
    $query = sprintf(Util::$insert_jornakText,
        mysql_real_escape_string($_POST['projectName']),
        mysql_real_escape_string(null),
        mysql_real_escape_string(null),
        mysql_real_escape_string(null),
        mysql_real_escape_string(null),
        mysql_real_escape_string(null)
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    //echo ("First query succesful. <br>");

    // Zapis do destination
    $newTextId = mysql_insert_id();
    $query = sprintf(Util::$insert_jornakDestinationPreview,
        mysql_real_escape_string($_POST['form_type']),
        mysql_real_escape_string($newTextId),
        //2011-04-11 16:01:31
        mysql_real_escape_string(date('Y-m-d H:i:s'))
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }

    Util::closeLink($link);
    header("location: ".Constants::$link_editProjects.$newTextId.'&newsection=true');
    exit();
}


/*if ($_FILES["video"]["error"] == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["video"]["tmp_name"];
    $name = $_FILES["video"]["name"];
    $fileVideo = "/project_".date('Ymdhis')."_".$name;
    $fullUploadDir = Constants::$uploads_dir.$fileVideo;
    move_uploaded_file($tmp_name, $fullUploadDir);
}else {
    $fileVideo = $_POST['videoHist'];
}

if ($_FILES["present"]["error"] == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["present"]["tmp_name"];
    $name = $_FILES["present"]["name"];
    $filePresent = "/present_".date('Ymdhis')."_".$name;
    $fullUploadDir = Constants::$uploads_dir.$filePresent;
    move_uploaded_file($tmp_name, $fullUploadDir);
    $filePresent = $filePresent.';'.$_POST['descPresent'];
}else {
    $filePresent = $_POST['exeHist'];
}*/
//ulozeni obrazku do databaze

$imgPrepare = explode(';',$_POST['images']);
//print_r($imgPrepare);
$j=0;
for ($i = 1; $i < 11; $i++) {
    if ($_FILES['img'.$i]['tmp_name'] == null) {
        $imgId[$i] = $imgPrepare[$j];
        $j=$j+2;
        continue;
    }
    $handle = fopen($_FILES['img'.$i]['tmp_name'], "rb");
    list($width, $height) = getimagesize($_FILES['img'.$i]['tmp_name']);
    $img = fread($handle, filesize($_FILES['img'.$i]['tmp_name']));
    fclose($handle);

    $img = base64_encode($img);

    $myArray = explode(".",$_FILES['img'.$i]['name']);

    $query = sprintf(Util::$insert_jornakImages,
        mysql_real_escape_string($img),
        mysql_real_escape_string($myArray[1]),
        mysql_real_escape_string($width),
        mysql_real_escape_string($height)
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    //echo ("Image uploaded succesfuly. <br>");

    $imgId[$i] = mysql_insert_id();
    $j=$j+2;
}

$empty = '';
for($i=1; $i<11;$i++) {
    if ($i == 1) {
        $firstImages = $imgId[$i].';' . $empty;
    }else {
        $firstImages = $firstImages.';'.$imgId[$i].';' . $empty;
    }
}
$filePresent = $_POST['present'].';'.$_POST['descPresent'];
// zapis textu
$query = sprintf(Util::$insert_jornakText,
    mysql_real_escape_string($_POST['header']),
    mysql_real_escape_string($_POST['text']),
    mysql_real_escape_string($_POST['list']),
    mysql_real_escape_string($_POST['video']),
    mysql_real_escape_string($firstImages),
    mysql_real_escape_string($filePresent)
);

// Perform Query
$result = mysql_query($query);

// Check result
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
//echo ("First query succesful. <br>");

// Zapis do destination
$newTextId = mysql_insert_id();

if ($_POST['element_17_1'] == 'visible') {
    $query = sprintf(Util::$insert_jornakDestination,
        mysql_real_escape_string($_POST['form_type']),
        mysql_real_escape_string($newTextId),
        //2011-04-11 16:01:31
        mysql_real_escape_string(date('Y-m-d H:i:s'))
    );
}else {
    $query = sprintf(Util::$insert_jornakDestinationPreview,
        mysql_real_escape_string($_POST['form_type']),
        mysql_real_escape_string($newTextId),
        //2011-04-11 16:01:31
        mysql_real_escape_string(date('Y-m-d H:i:s'))
    );
}

// Perform Query
$result = mysql_query($query);

// Check result
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

$query = sprintf(Util::$delete_jornakDestinationByTextId,
    mysql_real_escape_string($_POST['textId'])
);

// Perform Query
$result = mysql_query($query);

// Check result
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}


if (isset($_POST['newSectionSecondStep'])) {
//create news

    $moreInfo = Constants::$fullLink.'project-detail.php?id='.$newTextId;
    $textWithLink = $_POST['text'] . 'more info here: ' . $moreInfo;
    // zapis textu
    $query = sprintf(Util::$insert_jornakText,
        mysql_real_escape_string($_POST['header']),
        mysql_real_escape_string($_POST['text']),
        mysql_real_escape_string($newTextId),
        mysql_real_escape_string(null),
        mysql_real_escape_string(null),
        mysql_real_escape_string($_POST['list'])
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }


    // Zapis do destination
    $query = sprintf(Util::$insert_jornakDestination,
        mysql_real_escape_string('1'),
        mysql_real_escape_string(mysql_insert_id()),
        //2011-04-11 16:01:31
        mysql_real_escape_string(Util::getCurrentDateTime())
    );

    // Perform Query
    $result = mysql_query($query);

    // Check result
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
    
    fibixCreateRSS();

    //Util::sendMail("News From Fibix - " . $_POST['header'], $textWithLink);
}



Util::closeLink($link);

header("location: ".Constants::$link_done_fromResolve);

?>